<?php
/////////////////////////////////////////////////////
/// 函数名称：dh_post_views 
/// 函数作用：记录文章读的次数
/// 函数作者: DH
/// 作者地址: http://dhblog.org/ 
/////////////////////////////////////////////////////

$Lc_archivers_path= ABSPATH . 'archivers/';
$Lc_archivers_src_path= ABSPATH . 'wp-content/themes/dhchannel/functions/archivers/';
$Lc_archivers_url= get_bloginfo('url'). '/archivers/';
//多长时间之内的文章需要更新
$Lc_lastupdate = '2009-09-14 08:40:00';
$Lc_category_index_all = '';

//页面显示的文章列表条目的个数
$limit = 20;

$Lc_head = '';
$Lc_side = '';
$Lc_foot = '';

function dh_post_views()
{
	dh_global_set();
	dh_copy_files();
	dh_process_index();
	dh_update_last();
}
add_action('publish_post', 'dh_post_views');
//dh_post_views();

function dh_global_set()
{
	global $Lc_head,$Lc_side,$Lc_foot,$Lc_archivers_src_path;
	
	$archivers_category_id = archivers_category_id(0);
	$archivers_tag = archivers_tag();
	$archivers_page = archivers_page();	
	
	$path_head_html  = $Lc_archivers_src_path . 'head.html';
	$path_side_html  = $Lc_archivers_src_path . 'side.html';
	$path_foot_html  = $Lc_archivers_src_path . 'foot.html';

	$Lc_head = dh_file_get_contents("$path_head_html");
	$Lc_side = dh_file_get_contents("$path_side_html");
	$Lc_foot = dh_file_get_contents("$path_foot_html");
	
	$Lc_side = str_replace("%Lc_category%",'分类目录',$Lc_side);
	$Lc_side = str_replace("%Lc_category_contents%",$archivers_category_id,$Lc_side);
	$Lc_side = str_replace("%Lc_tag%",'讨论主题',$Lc_side);
	$Lc_side = str_replace("%Lc_tag_contents%",$archivers_tag,$Lc_side);
	
	$Lc_head = str_replace("%Lc_blog_name%",get_bloginfo('name'),$Lc_head);
	//$Lc_head = str_replace("%Lc_keywords%",get_bloginfo('name'),$Lc_head);
	//$Lc_head = str_replace("%Lc_descriptions%",get_option('blogdescription'),$Lc_head);
	$Lc_head = str_replace("%Lc_page_contents%",$archivers_page,$Lc_head);
	$Lc_head = str_replace("%Lc_the_header%",get_option('blogdescription'),$Lc_head);
}

//拷贝style.css等静态文件到发布目录
function dh_copy_files()
{
	global $Lc_archivers_path,$Lc_archivers_src_path;
	$stylesrc = $Lc_archivers_src_path . 'style.css';
	$styledest = $Lc_archivers_path . 'style.css';
	$bgsrc = $Lc_archivers_src_path . 'bg.png';
	$bgdest = $Lc_archivers_path . 'bg.png';
	$title_bgsrc = $Lc_archivers_src_path . 'title_bg.jpg';
	$title_bgdest = $Lc_archivers_path . 'title_bg.jpg';
	copy($stylesrc,$styledest);
	copy($bgsrc,$bgdest);
	copy($title_bgsrc,$title_bgdest);
}

//处理Lc_lastupdate时间之类的文章
function dh_update_last()
{
	global $wpdb,$Lc_lastupdate;
	$sql = "SELECT DISTINCT ID
		FROM $wpdb->posts
		LEFT JOIN $wpdb->comments ON ( $wpdb->posts.ID = $wpdb->comments.comment_post_ID ) 
		WHERE post_password = ''
		AND (post_type = 'post' or post_type = 'page' )
		AND post_status = 'publish'
		AND ( post_date > '$Lc_lastupdate'
		OR post_modified > '$Lc_lastupdate'
		OR comment_date > '$Lc_lastupdate' )
		ORDER BY post_date DESC 
		";
	$update_ID = $wpdb->get_results($sql);

	if($update_ID) {
		foreach($update_ID as $post) {
			$post_ID = $post->ID;
			###update the post
			update_the_post($post->ID);
			###update post list
			//cat
			if(get_the_category($post->ID)){
				 foreach(get_the_category($post->ID) as $category) { 
					//echo Lc_get_term_count($cat_id);
					update_cat_post_list($category->cat_ID);
				}
			}
			//tags
			if(get_the_tags($post_ID)){
				foreach(get_the_tags($post_ID) as $tag) {
					update_tag_post_list($tag->slug,$tag->term_id);
				 }
			}
		}
	} 
}

////处理index.html
function dh_process_index()
{
	global $Lc_archivers_path,$Lc_archivers_src_path;
	//定义输入的index.html的模板的位置
	$path_index_html  = $Lc_archivers_src_path . 'content_index.html';
	//定义输出的index.html的位置
	$file_index_html = $Lc_archivers_path . 'index.html';

	//打开文件，取出文件内容
	$index_html = dh_file_get_contents("$path_index_html");
	$index_html = dh_replace_html($index_html);
	//替换固定字段
	$index_html = str_replace("%Lc_lastest_post%",'最近发表',$index_html);
	$index_html = str_replace("%Lc_lastest_post_contents%",archivers_lastest_post(),$index_html);
	$index_html = str_replace("%Lc_hotest_post%",'最多评论',$index_html);
	$index_html = str_replace("%Lc_hotest_post_contents%",archivers_hotest_post(),$index_html);
	global $Lc_head,$Lc_side,$Lc_foot;	
	//index页面的头信息
	$Lc_head_index = $Lc_head;
	$Lc_head_index = str_replace("%Lc_blog_title%",get_bloginfo('name').'的归档首页',$Lc_head_index);
	$Lc_head_index = str_replace("%Lc_keywords%",get_bloginfo('name').'的归档首页',$Lc_head_index);
	$Lc_head_index = str_replace("%Lc_descriptions%",get_option('blogdescription'),$Lc_head_index);
	$index_html = $Lc_head_index.$index_html.$Lc_side.$Lc_foot;
	if (!file_exists($Lc_archivers_path))  
	{   
		mkdir($Lc_archivers_path,0777);
	} 
	dh_file_put_contents("$file_index_html","$index_html"); 
}

//根据id刷新文章和分类、tag页面
function update_the_post($post_ID){
	global $wpdb,$Lc_archivers_path,$Lc_archivers_src_path,$Lc_archivers_url;
	$path_index_html  = $Lc_archivers_src_path.'content_post.html';
	$file_post_html = $Lc_archivers_path.'p'.$post_ID.'.html';

	$post_html = file_get_contents("$path_index_html");
	$sql_post = "SELECT ID, post_author, post_date, post_content, post_title, post_excerpt,guid
				 FROM $wpdb->posts 
				 WHERE ID = '$post_ID' ";
	$get_update_post = $wpdb->get_results($sql_post);
	if($get_update_post){
		foreach($get_update_post as $post) {
			$post_ID = $post->ID;
			$post_date = $post->post_date;
			$post_author = $post->post_author;
			//$post_content_r = wpautop($post->post_content,1);
			$post_content_r = pre_filter($post->post_content);
			$Lc_post_title = $post->post_title;
			$post_excerpt = $post->post_excerpt;
			$Lc_post_base = $post->guid;
		}
	}    
	###post comments
	$sql_comments = "SELECT comment_ID, comment_author, comment_date, comment_content
	                 FROM $wpdb->comments
					 WHERE comment_post_ID = '$post_ID'
					 AND comment_approved = '1'
					 ORDER BY comment_ID";
	$get_comments = $wpdb->get_results($sql_comments);
	$Lc_comments = '';
	if($get_comments){
		foreach($get_comments as $comments) {
			$comments_p = str_replace(array('%1$s','%2$s','%3$s'), array("$comments->comment_author", "$comments->comment_date", ""),'作者：<strong>%1$s</strong> 时间： %2$s %3$s');
			$Lc_comments = $Lc_comments.'<div id="comment"><div id="comment_title">'.$comments_p.'</div><div id="comment_content">'.$comments->comment_content.'</div></div>';
		} 
	}
    
    ###post category 
	$category = get_the_category($post_ID); 
	$last_category = array_pop($category);
    $Lc_post_cat= $last_category->cat_name;
	$Lc_post_cat_url = $Lc_archivers_url.'cat'.$last_category->cat_ID.'-1.html';

    ###post tags
	$Lc_post_tag ='';
	$Lc_post_tag_k ='';
	if($get_the_tags = get_the_tags($id = $post_ID)) {
		foreach($get_the_tags as $tag) {
			$Lc_post_tag = $Lc_post_tag.'<a href="'.get_tag_link($tag->term_id).'" title="'.$tag->name.'">'.$tag->name.'</a>'.", ";
		}
		$Lc_post_tag = rtrim($Lc_post_tag,', '); 
		$Lc_post_tag_k = strip_tags($Lc_post_tag);
	}
	
	$post_html = dh_replace_html($post_html);
	$post_html = str_replace("%Lc_comments%",$Lc_comments,$post_html);
	$post_html = str_replace("%Lc_post_content%",$post_content_r,$post_html);
	$post_html = str_replace("%Lc_post_title%",$Lc_post_title,$post_html);
	$post_html = str_replace("%Lc_post_p%",$post_date,$post_html);
	$post_html = str_replace("%Lc_post_cat%",$Lc_post_cat,$post_html);
	$post_html = str_replace("%Lc_post_cat_url%",$Lc_post_cat_url,$post_html);
	$post_html = str_replace("%Lc_post_base%",$Lc_post_base,$post_html);
	global $Lc_head,$Lc_side,$Lc_foot;
	//post 页面的头信息
	$Lc_head_post = $Lc_head;
	$Lc_head_post = str_replace("%Lc_blog_title%",$Lc_post_title,$Lc_head_post);
	$Lc_head_post = str_replace("%Lc_keywords%",$Lc_post_title,$Lc_head_post);
	$Lc_head_post = str_replace("%Lc_descriptions%",$Lc_post_title,$Lc_head_post);	
	$post_html = $Lc_head_post.$post_html.$Lc_side.$Lc_foot;
	
	if (!file_exists($Lc_archivers_path))  
	{   
		mkdir($Lc_archivers_path,0777);
	} 
	file_put_contents("$file_post_html","$post_html"); 
}

###get the post_list
function update_cat_post_list($vaule) 
{
	global $wpdb,$post,$limit,$Lc_archivers_path,$Lc_archivers_src_path,$Lc_archivers_url; 	
	global $Lc_head,$Lc_side,$Lc_foot;	
	$myposts = get_posts("category=$vaule&numberposts=-1&post_status=publish&post_type=post"); 

	###options
	$Lc_post_term = get_cat_name($vaule);
	$Lc_post_term_permalink = get_category_link($vaule);
	###End options

	$ii = 0;
	$jj=1;
	foreach ($myposts as $post)
	{
		 $ii++;
		 $limit_page = $jj;
		 if($ii == $limit) { $jj++; $ii = 0; }
		 $comment_count= Lc_get_post_count($post->ID);
		 $Lc_post_term_contents_{$limit_page} = $Lc_post_term_contents_{$limit_page}.'<li><a href="'. $Lc_archivers_url.'p'.$post->ID.'.html" title="'.$post->post_title.'" >'.$post->post_title.'</a> ('.str_replace('%s',$comment_count,__('Comments %s')).')</li>'."\n";
		 $Lc_post_term_url_{$limit_page} = $Lc_archivers_url.'cat'.$vaule.'-'.$limit_page.'.html';
	 }

	 //更新每个页面
	 for($j=1;$j<=$limit_page;$j++)
	 {
		$Lc_limit_page = '';
		$Lc_post_term_title = $Lc_post_term.'('.__('Pages').$j.')';
		$Lc_post_term_url = $Lc_post_term_url_{1};
		$Lc_post_term_contents = $Lc_post_term_contents_{$j};
		
		$path_post_list_html  = $Lc_archivers_src_path.'content_post_list.html';
		$post_list_html = file_get_contents("$path_post_list_html");
		
		$post_list_html = dh_replace_html($post_list_html);
		$post_list_html = str_replace("%Lc_post_term_contents%",$Lc_post_term_contents,$post_list_html);
		$post_list_html = str_replace("%Lc_post_term%",$Lc_post_term,$post_list_html);
		$post_list_html = str_replace("%Lc_post_term_title%",$Lc_post_term_title,$post_list_html);
		//更新page_navi
		for($i=1;$i<=$limit_page;$i++)
		{
			 if($i==$j)
				$Lc_limit_page_r = '['.$i.'] ';
			 else
				$Lc_limit_page_r = '['.'<a href="'.$Lc_post_term_url_{$i}.'">'.$i.'</a>'.']'.' ';
			 $Lc_limit_page .= $Lc_limit_page_r;  
		 }		
		$post_list_html = str_replace("%Lc_limit_page%",$Lc_limit_page,$post_list_html);		
		$post_list_html = str_replace("%Lc_post_term_url%",$Lc_post_term_url,$post_list_html);
		$file_post_list_html = $Lc_archivers_path.'cat'.$vaule.'-'.$j.'.html';
		//post_list cat 页面的头信息
		$Lc_head_post_list = $Lc_head;
		$Lc_head_post_list = str_replace("%Lc_blog_title%",get_bloginfo('name').' 分类:'.$Lc_post_term_title,$Lc_head_post_list);
		$Lc_head_post_list = str_replace("%Lc_keywords%",'分类:'.$Lc_post_term_title,$Lc_head_post_list);
		$Lc_head_post_list = str_replace("%Lc_descriptions%",'分类:'.$Lc_post_term_title,$Lc_head_post_list);	
		
		$post_list_html = $Lc_head_post_list.$post_list_html.$Lc_side.$Lc_foot;
		
		if (!file_exists($Lc_archivers_path))  
		{   
			mkdir($Lc_archivers_path,0777);
		} 
		file_put_contents("$file_post_list_html","$post_list_html"); 					
	 }
}

function update_tag_post_list($vaule,$tagid) 
{
	global $wpdb,$post,$limit,$Lc_archivers_path,$Lc_archivers_src_path,$Lc_archivers_url; 	
	global $Lc_head,$Lc_side,$Lc_foot;	
	$myposts = get_posts("tag=$vaule&numberposts=-1&post_status=publish&post_type=post");

	$Lc_post_term = get_tag_name($tagid);

	$ii = 0;
	$jj=1;
	foreach ($myposts as $post)
	{
		 $ii++;
		 $limit_page = $jj;
		 if($ii == $limit) { $jj++; $ii = 0; }
		 $comment_count= Lc_get_post_count($post->ID);
		 $Lc_post_term_contents_{$limit_page} = $Lc_post_term_contents_{$limit_page}.'<li><a href="'. $Lc_archivers_url.'p'.$post->ID.'.html" title="'.$post->post_title.'" >'.$post->post_title.'</a> ('.str_replace('%s',$comment_count,__('Comments %s')).')</li>'."\n";
		 $Lc_post_term_url_{$limit_page} = $Lc_archivers_url.'tag'.$tagid.'-'.$limit_page.'.html';
	 }

	 //更新每个页面
	 for($j=1;$j<=$limit_page;$j++)
	 {
		$Lc_limit_page = '';
		$Lc_post_term_title = $Lc_post_term.'('.__('Pages').$j.')';
		$Lc_post_term_url = $Lc_post_term_url_{1};
		$Lc_post_term_contents = $Lc_post_term_contents_{$j};
		
		$path_post_list_html  = $Lc_archivers_src_path.'content_post_list.html';
		$post_list_html = file_get_contents("$path_post_list_html");
		
		$post_list_html = dh_replace_html($post_list_html);
		$post_list_html = str_replace("%Lc_post_term_contents%",$Lc_post_term_contents,$post_list_html);
		$post_list_html = str_replace("%Lc_post_term%",$Lc_post_term,$post_list_html);
		$post_list_html = str_replace("%Lc_post_term_title%",$Lc_post_term_title,$post_list_html);
		//更新page_navi
		for($i=1;$i<=$limit_page;$i++)
		{
			 if($i==$j)
				$Lc_limit_page_r = '['.$i.'] ';
			 else
				$Lc_limit_page_r = '['.'<a href="'.$Lc_post_term_url_{$i}.'">'.$i.'</a>'.']'.' ';
			 $Lc_limit_page .= $Lc_limit_page_r;  
		 }		
		$post_list_html = str_replace("%Lc_limit_page%",$Lc_limit_page,$post_list_html);	
		$post_list_html = str_replace("%Lc_post_term_url%",$Lc_post_term_url,$post_list_html);		
		$file_post_list_html = $Lc_archivers_path.'tag'.$tagid.'-'.$j.'.html';
		//post_list tag 页面的头信息
		$Lc_head_post_list = $Lc_head;
		$Lc_head_post_list = str_replace("%Lc_blog_title%",get_bloginfo('name').' 标签:'.$Lc_post_term_title,$Lc_head_post_list);
		$Lc_head_post_list = str_replace("%Lc_keywords%",'标签:'.$Lc_post_term_title,$Lc_head_post_list);
		$Lc_head_post_list = str_replace("%Lc_descriptions%",'标签:'.$Lc_post_term_title,$Lc_head_post_list);			
		
		$post_list_html = $Lc_head_post_list.$post_list_html.$Lc_side.$Lc_foot;		
		if (!file_exists($Lc_archivers_path))  
		{   
			mkdir($Lc_archivers_path,0777);
		} 
		file_put_contents("$file_post_list_html","$post_list_html"); 					
	 }
}

function archivers_tag() {
    global $wpdb, $posts, $wp_version,$Lc_archivers_url;
	$Lc_archivers_catalog = 'archivers';
	$category_index_all='';
    
	$sql_category = "SELECT $wpdb->terms.term_id as cat_ID, name, slug, count, description
					FROM $wpdb->terms,$wpdb->term_taxonomy
					WHERE $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id and 
						  taxonomy = 'post_tag'";
	$category_index = $wpdb->get_results($sql_category);
	if($category_index) {
		foreach($category_index as $category) {
			$category_name = $category->name;
			$category_slug = $category->slug;
			$category_count = $category->count;
			$category_Id = $category->cat_ID;
			$category_description = $category->description;
			if($category_count){
                $category_index_r = '<li><a href="'.$Lc_archivers_url.'tag'.$category_Id.'-1.html" title="'.$category_description.'">'.$category_name.'</a> ('.$category_count.')</li>';
                $category_index_all .=  $category_index_r."\n";
			}
		}
	} 
   return $category_index_all;
}

function archivers_pages() {
    global $wpdb, $posts, $wp_version,$Lc_archivers_url;
	$Lc_archivers_catalog = 'archivers';
	$category_index_all='';
    
	$sql_category = "SELECT $wpdb->terms.term_id as cat_ID, name, slug, count, description
					FROM $wpdb->terms,$wpdb->term_taxonomy
					WHERE $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id and 
						  taxonomy = 'post_tag'";
	$category_index = $wpdb->get_results($sql_category);
	if($category_index) {
		foreach($category_index as $category) {
			$category_name = $category->name;
			$category_slug = $category->slug;
			$category_count = $category->count;
			$category_Id = $category->cat_ID;
			$category_description = $category->description;
			if($category_count){
                $category_index_r = '<li><a href="'.$Lc_archivers_url.'tag'.$category_Id.'-1.html" title="'.$category_description.'">'.$category_name.'</a> ('.$category_count.')</li>';
                $category_index_all .=  $category_index_r."\n";
			}
		}
	} 
   return $category_index_all;
}

function archivers_category() 
{
    global $wpdb, $Lc_archivers_url;
	$Lc_archivers_catalog = 'archivers';
	$category_index_all='';
    
	$sql_category = "SELECT $wpdb->terms.term_id as cat_ID, name, slug, count, description
					FROM $wpdb->terms,$wpdb->term_taxonomy
					WHERE $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id and 
						  taxonomy = 'category'";
	$category_index = $wpdb->get_results($sql_category);
	if($category_index) {
		foreach($category_index as $category) {
			$category_name = $category->name;
			$category_slug = $category->slug;
			$category_count = $category->count;
			$category_Id = $category->cat_ID;
			$category_description = $category->description;
			if($category_count){
                $category_index_r = '<li><a href="'.$Lc_archivers_url.'cat'.$category_Id.'-1.html" title="'.$category_description.'">'.$category_name.'</a> ('.$category_count.')</li>';
                $category_index_all .=  $category_index_r."\n";
			}
		}
	} 
   //return $category_index_all;
   return wp_list_categories('title_li=&orderby=name&use_desc_for_title=false&exclude=1&echo=0');
}

function archivers_category_id($cat_ID)
{
    global $wpdb,$Lc_category_index_all,$Lc_archivers_url;
	$sql_category = "SELECT $wpdb->terms.term_id as cat_ID, name, slug, count, description
					FROM $wpdb->terms,$wpdb->term_taxonomy
					WHERE $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id and 
						  taxonomy = 'category' and parent = $cat_ID";
	$category_index = $wpdb->get_results($sql_category);
	if($category_index) {
		$Lc_category_index_all .= '<ul>';
		foreach($category_index as $category) {
			
			$category_name = $category->name;
			$category_slug = $category->slug;
			$category_count = $category->count;
			$category_Id = $category->cat_ID;
			$category_description = $category->description;			
			$category_index_r = '<li><a href="'.$Lc_archivers_url.'cat'.$category_Id.'-1.html" title="'.$category_description.'">'.$category_name.'</a> ('.$category_count.')';
            $Lc_category_index_all .=  $category_index_r;
			archivers_category_id($category->cat_ID);
			$Lc_category_index_all .= '</li>';
		}
		$Lc_category_index_all .= '</ul>';
	} 
	return $Lc_category_index_all;
}

function archivers_page() 
{
	global $wpdb,$Lc_archivers_src_path;
	$archivers_page_all = '';
	$sql_ap = "SELECT ID, post_title
              FROM $wpdb->posts
              WHERE post_type = 'page'
              AND post_status = 'publish'
              AND post_password = ''";
    $archivers_page_index = $wpdb->get_results($sql_ap);
	if($archivers_page_index) { 
		foreach ($archivers_page_index as $page) {
            $archivers_page_url = $Lc_archivers_path.'p'.$page->ID.'.html';
            $archivers_page_title = $page->post_title;
            $archivers_page_r = '<li><a href="'.$archivers_page_url.'" title="'.$archivers_page_title.'">'.$archivers_page_title.'</a></li>';
			$archivers_page_all .= $archivers_page_r."\n";
		}
	}
	 return $archivers_page_all;
}

function archivers_lastest_post()
{
	global $wpdb,$Lc_archivers_url;
	$recent_post_all = '';
	$sql_rp = "SELECT ID, post_title, comment_count
              FROM $wpdb->posts
              WHERE post_type = 'post'
              AND post_status = 'publish'
              AND post_password = ''
              ORDER BY ID DESC 
              LIMIT 0, 10";
	$get_recent_post = $wpdb->get_results($sql_rp);
	if($get_recent_post) {
		foreach($get_recent_post as $post) {
           //$recent_post_url = get_permalink($post->ID);
		   $recent_post_url = $Lc_archivers_url.'p'.$post->ID.'.html';
		   $comment_count = $post->comment_count;
		   $recent_post_r = '<li><a href="'.$recent_post_url.'" title="'.$post->post_title.'">'.$post->post_title.'</a> ('.str_replace('%s',$comment_count,__('Comments %s')).')</li>';
           $recent_post_all .= $recent_post_r."\n";
		}
	}
	return $recent_post_all;
}

function archivers_hotest_post()
{
	global $wpdb,$Lc_archivers_url;
	$recent_post_all = '';
	$sql_rp = "SELECT ID,post_title,comment_count FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0, 10";
	$get_recent_post = $wpdb->get_results($sql_rp);
	if($get_recent_post) {
		foreach($get_recent_post as $post) {
           //$recent_post_url = get_permalink($post->ID);
		   $recent_post_url = $Lc_archivers_url.'p'.$post->ID.'.html';
		   $comment_count = $post->comment_count;
		   $recent_post_r = '<li><a href="'.$recent_post_url.'" title="'.$post->post_title.'">'.$post->post_title.'</a> ('.str_replace('%s',$comment_count,__('Comments %s')).')</li>';
           $recent_post_all .= $recent_post_r."\n";
		}
	}
	return $recent_post_all;
}

//替换固定的字段
function dh_replace_html($html)
{
	global $Lc_archivers_url;
	//替换固定字段
	$html = str_replace("%Lc_blog_home%",$Lc_archivers_url,$html);
	$html = str_replace("%Lc_blog_name%",get_bloginfo('name'),$html);
	return $html;
}

function Lc_get_post_count($post_id) 
{
	 global $wpdb;
      $sql ="SELECT comment_count
	         From $wpdb->posts
			 where ID = '$post_id'";
	  $mycount = $wpdb->get_results($sql);
	  if($mycount){
		  foreach($mycount as $count){
			  $Lc_get_post_count = $count->comment_count;
		  }
	  }
      return $Lc_get_post_count;
}
function dh_file_get_contents($filename) {
	$fh = fopen($filename, 'rb', false);

	clearstatcache();
	if ($fsize = @filesize($filename)) {
		$data = fread($fh, $fsize);
	} else {
		$data = '';
		while (!feof($fh)) {
			$data .= fread($fh, 8192);
		}
	}
	fclose($fh);
	return $data;
}

function dh_file_put_contents($filename, $content) {
	
	// Open the file for writing
	$fh = @fopen($filename, 'wb', false);
	// Write to the file
	@fwrite($fh, $content);
	// Close the handle
	@fclose($fh);
}

function get_tag_name($tagid)
{
	global $wpdb;
    $sql ="SELECT name From $wpdb->terms where term_id = '$tagid'";
	$mytags = $wpdb->get_results($sql);
	if($mytags){
		foreach($mytags as $tag){
		  $Lc_get_tag_name = $tag->name;
		}
	}
    return $Lc_get_tag_name;
}

function pre_filter($content)
{
	return preg_replace_callback(
        "/\s*<pre(?:lang=[\"']([\w-]+)[\"']|line=[\"'](\d*)[\"']|escaped=[\"'](true|false)?[\"']|\s)+>(.*)<\/pre>\s*/siU",
        "wp_syntax_substitute", $content);
}
?>